const vscode = require('vscode');
const os = require('os');
const path = require('path');
const fs = require('fs');

function getStoragePath() {
    // 首先检查用户是否在设置中指定了路径
    const config = vscode.workspace.getConfiguration('cursorFreeMachine');
    const customPath = config.get('storagePath');

    if (customPath && fs.existsSync(customPath)) {
        return customPath;
    }

    // 如果没有指定或路径无效，使用默认路径
    const platform = os.platform();
    let basePath;

    switch (platform) {
        case 'win32':
            basePath = path.join(os.homedir(), 'AppData', 'Roaming', 'Cursor', 'User', 'globalStorage');
            break;
        case 'darwin':
            basePath = path.join(os.homedir(), 'Library', 'Application Support', 'Cursor', 'User', 'globalStorage');
            break;
        case 'linux':
            basePath = path.join(os.homedir(), '.config', 'Cursor', 'User', 'globalStorage');
            break;
        default:
            throw new Error('不支持的操作系统');
    }

    return path.join(basePath, 'storage.json');
}

function modifyMacMachineId() {
    try {
        const storagePath = getStoragePath();

        // 检查文件是否存在
        if (!fs.existsSync(storagePath)) {
            throw new Error(`文件不存在: ${storagePath}`);
        }

        // 读取文件
        let data = JSON.parse(fs.readFileSync(storagePath, 'utf8'));

        // 获取用户配置的 machineId 或生成随机 ID
        const config = vscode.workspace.getConfiguration('cursorFreeMachine');
        const customMachineId = config.get('customMachineId');
        const newMachineId = customMachineId || generateRandomMachineId();

        // 修改 telemetry.macMachineId
        if (!data.telemetry) {
            data.telemetry = {};
        }
        data["telemetry.devDeviceId"] = newMachineId;
        const newMachine42Id = newMachineId + generateRandom42Id();
        data["telemetry.macMachineId"] = newMachine42Id;
        data["telemetry.machineId"] = newMachine42Id;

        // 写回文件
        fs.writeFileSync(storagePath, JSON.stringify(data, null, 2), 'utf8');

        return {
          success: true,
          message: "已成功修改 telemetry.devDeviceId、macMachineId、machineId",
          devDeviceId: newMachineId,
          macMachineId: newMachine42Id,
          machineId: newMachine42Id,
          path: storagePath,
        };
    } catch (error) {
        throw new Error(`修改失败: ${error.message}`);
    }
}

function generateRandomMachineId() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0;
    const v = c === "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}

function generateRandom42Id() {
  return "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".replace(
    /[xy]/g,
    function (c) {
      const r = (Math.random() * 16) | 0;
      const v = c === "x" ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    }
  );
}

function activate(context) {
    let disposable = vscode.commands.registerCommand(
      "cursor-free-machine.cursor-free-machine",
      async function () {
        try {
          const result = modifyMacMachineId();
          vscode.window.showInformationMessage(
            `修改成功！\n路径: ${result.path}\n新的 devDeviceId: ${result.devDeviceId}\n新的 macMachineId: ${result.macMachineId}\n新的 machineId: ${result.machineId}`
          );
        } catch (error) {
          vscode.window.showErrorMessage(`修改失败: ${error.message}`);

          // 如果是路径不存在的错误，提示用户设置路径
          if (error.message.includes("不存在")) {
            const answer = await vscode.window.showQuestionMessage(
              "是否要打开设置页面指定 storage.json 的路径？",
              "是",
              "否"
            );
            if (answer === "是") {
              vscode.commands.executeCommand(
                "workbench.action.openSettings",
                "cursorFreeMachine.storagePath"
              );
            }
          }
        }
      }
    );

    context.subscriptions.push(disposable);
}

function deactivate() {}

module.exports = {
    activate,
    deactivate,
};
